<include file="header" />
<body>
<div class="wraper">
	<div class="head clearfix">
		<a href="{BEESCRM::U('/base/UserCenter/addressmanage',array('account_id'=>$accountId))}" class="iconfont icon-zuo pull-left"></a>
		<span class="head-name pull-left">地址管理</span>
		<if condition="$addressId">
			<a href="javacritp:void(0);" class="pull-left text-right del">删除</a>
		</if>
	</div>
	<if condition="$userType eq 0">
		<div class="lists">
			<select name="community_id" id="community_id">
				<option value="0">请选择您所在的园区</option>
				<foreach name="community" item="vo">
					<option value="{BEESCRM:$vo.community_id}" data-name="{BEESCRM:$vo.community_name}" <if condition="$addressInfo['community_id'] eq $vo['community_id']">selected</if> >{BEESCRM:$vo.community_name}</option>
				</foreach>
			</select>
			<span class="triangle"></span>
		</div>
		<div class="lists">
			<select name="building_id" id="building_id">
				<option value="0">请选择您所在的楼幢</option>


			</select>
			<span class="triangle"></span>
		</div>
		<div class="lists">
			<select name="floor_id" id="floor_id">
				<option value="0">请选择您所在的楼层</option>

			</select>
			<span class="triangle"></span>
		</div>
		<div class="lists">
			<select name="room_id" id="room_id">
				<option value="0">请选择您所在的寝室</option>
			</select>
			<span class="triangle"></span>
		</div>
	</if>

	<div class="lists">
		<input placeholder="请输入姓名" name="real_name" id="real_name" value="{BEESCRM:$addressInfo.real_name}">
	</div>
	<div class="lists">
		<input placeholder="请输入手机号码" name="mobile" id="mobile" value="{BEESCRM:$addressInfo.mobile}">
	</div>
	<if condition="$userType neq 0">
		<div class="lists">
			<input placeholder="请输入地址" name="address_detail" id="address_detail_container" value="{BEESCRM:$addressInfo.address_detail}">
		</div>
	</if>
	<div class="lists">
		<input placeholder="请输入标签类别" name="label" id="label" value="{BEESCRM:$addressInfo.label}">
	</div>
	<div class="btn-box">
		<a href="javascript:void(0);" class="btn" id="addAddress" data-id="{BEESCRM:$addressInfo.address_id}">确认保存</a>
	</div>

</div>

<input type="hidden" name="addressId" id="addressId" value="{BEESCRM:$addressId}" />
<input type="hidden" name="userType" id="userTypeContainer" value="{BEESCRM:$userType}" />

<script type="text/javascript" src="/assets/base/userCenter/modules/sea.js"></script>

<script enter="/assets/base/userCenter/static/publicjs/public" id="confnode" src="/assets/base/userCenter/static/publicjs/config.js"></script>
<script>
	seajs.use('$', function(){
		//初始化
		var curCommunityId = "{BEESCRM:$addressInfo['community_id']}";
		var curBuildingId = "{BEESCRM:$addressInfo['building_id']}";
		var curFloorId = "{BEESCRM:$addressInfo['floor_id']}";
		var curRoomId = "{BEESCRM:$addressInfo['room_id']}";
		if(curCommunityId){
			var bList = getBuilding(curCommunityId);
			var options = [];
			var option = '<option value="0">请选择您所在的楼幢</option>';
			options.push(option);
			if (bList) {
				$.each(bList, function (index, node) {
					if(node.building_id == curBuildingId){
						var option = '<option selected="selected" value="' + node.building_id + '" data-name="' + node.building_name + '" data-num="' + node.building_code + '">' + node.building_name + '</option>';
					}else{
						var option = '<option value="' + node.building_id + '" data-name="' + node.building_name + '" data-num="' + node.building_code + '">' + node.building_name + '</option>';
					}
					
					options.push(option);
				});
			}
			$("#building_id").html(options);
		}
		if(curBuildingId){
			var floorList = getFloor(curBuildingId);
			var options = [];
			var option = '<option value="0">请选择您所在的楼层</option>';
			options.push(option);
			if (floorList) {
				$.each(floorList, function (index, node) {
					if(node.floor_id == curFloorId){
						var option = '<option selected="selected" value="' + node.floor_id + '" data-name="' + node.floor_name + '">' + node.floor_name + '</option>';
					}else{
						var option = '<option value="' + node.floor_id + '" data-name="' + node.floor_name + '">' + node.floor_name + '</option>';
					}
					
					options.push(option);
				});
			}
			$("#floor_id").html(options);
		}
		if(curFloorId){
			var roomList = getRoom(curFloorId);
			var options = [];
			var option = '<option value="0">请选择您所在的寝室</option>';
			options.push(option);
			if (roomList) {
				$.each(roomList, function (index, node) {
					if(node.room_id == curRoomId){
						var option = '<option selected="selected value="' + node.room_id + '" data-name="' + node.room_name + '">' + node.room_name + '</option>';
					}else{
						var option = '<option value="' + node.room_id + '" data-name="' + node.room_name + '">' + node.room_name + '</option>';
					}
					
					options.push(option);
				});
			}
			$("#room_id").html(options);
		}

		//点击删除按钮
		$(document).on('click', '.del', function () {
			fm.confirm('确定要删除该地址?',function(n){
				if(n==0){
					return;
				}
				var data = {};
				data.addressId = $("#addressId").val();
				$.ajax({
					url: "{BEESCRM::U('/base/UserCenter/delAddress',array('account_id'=>$accountId))}",
					type: "post",
					dataType: 'json',
					async: false,
					data: data,
					success: function (data) {
						if (data.status == 'S') {
							fm.alert(data.message, '', 2, function () {
								location.href = "{BEESCRM::U('/base/UserCenter/addressManage',array('account_id'=>$accountId))}";
							})
						} else {
							fm.alert(data.message);
						}

					}
				});
			})

		});

		$(document).on('click', '#addAddress', function () {
			var json = {};
			var data = {};
			var verify_method = /^(?:13\d|15\d|18\d|17\d|14\d)\d{5}(\d{3}|\*{3})$/;

			data.address_id = $(this).data('id');
			data.room_id = $('#room_id').val();
			data.user_type = $("#userTypeContainer").val();
			if(data.user_type == 0){
				if (typeof($("#community_id option:selected").data('name')) == 'undefined') {
					data.community_name = '';
					fm.alert('请选择园区');return false;
				} else {
					data.community_id = $("#community_id option:selected").val();
					data.community_name = $("#community_id option:selected").data('name');
				}

				if (typeof($("#building_id option:selected").data('name')) == 'undefined') {
					data.building_name = '';
					fm.alert('请选择楼栋');return false;
				} else {
					data.building_id = $("#building_id option:selected").val();
					data.building_name = $("#building_id option:selected").data('name');
				}

				if (typeof($("#floor_id option:selected").data('name')) == 'undefined') {
					data.floor = '';
					fm.alert('请选择楼层');return false;
				} else {
					data.floor_id = $("#floor_id option:selected").val();
					data.floor_name = $("#floor_id option:selected").data('name');
				}

				if (typeof($("#room_id option:selected").data('name')) == 'undefined') {
					data.room = '';
					fm.alert('请选择房间');
					return false;
				} else {
					data.room_id = $("#room_id option:selected").val();
					data.room_name = $("#room_id option:selected").data('name');
				}
			}

			data.real_name = $("#real_name").val();
			if (!data.real_name) {
				fm.alert('请输入姓名~');
				return false;
			}

			data.mobile = $('#mobile').val();
			if (!data.mobile) {
				fm.alert('手机号不能为空');
				return false;
			} else if (!verify_method.test(data.mobile)) {
				fm.alert('请输入正确的手机号');
				return false;
			}

			if(data.user_type != 0){
				data.address_detail = $("#address_detail_container").val();
				if(!data.address_detail){
					fm.alert('请输入地址');
					return false;
				}
			}

			data.label = $("#label").val();
			if (data.label && data.label.length > 6) {
				fm.alert('标签');
				return false;
			}

			$.ajax({
				url: "{BEESCRM::U('/base/UserCenter/editAddress',array('account_id'=>$accountId))}",
				type: "post",
				dataType: 'json',
				async: false,
				data: data,
				success: function (data) {
					if (data.status == 'S') {
						fm.alert(data.message, '', 2, function () {
							location.href = "{BEESCRM::U('/base/UserCenter/addressmanage',array('account_id'=>$accountId))}";
						})
					} else {
						fm.alert(data.message);
					}

				}
			});
		})

		$('#community_id').on('change',function() {
			var community_id = $('#community_id').val();
			$('#floor_id').html('<option value="0">请选择您所在的楼层</option>');
			$('#room_id').html('<option value="0">请选择您所在的寝室</option>');
			var building = getBuilding(community_id);
			var options = [];
			var option = '<option value="0">请选择您所在的楼幢</option>';
			options.push(option);
			if (building) {
				$.each(building, function (index, node) {
					var option = '<option value="' + node.building_id + '" data-name="' + node.building_name + '" data-num="' + node.building_code + '">' + node.building_name + '</option>';
					options.push(option);
				});
			}
			$("#building_id").html(options);
		});

		function getBuilding(id) {
			if (id > 0) {
				var result = null;
				$.ajax({
					url: "{BEESCRM::U('Common/getBuildingList',array('account_id'=>$accountId))}",
					type: "post",
					dataType: 'json',
					async: false,
					data: {id: id},
					success: function (data) {
						if (data.status == 'S') {
							result = data.data;
						}
					}
				});
				return result;
			}
		}

		$('#building_id').change(function () {
			var building_id = $('#building_id').val();
			var floor = getFloor(building_id);
			var options = [];
			var option = '<option value="0">请选择您所在的楼层</option>';
			options.push(option);
			if (floor) {
				$.each(floor, function (index, node) {
					var option = '<option value="' + node.floor_id + '" data-name="' + node.floor_name + '">' + node.floor_name + '</option>';
					options.push(option);
				});
			}
			$("#floor_id").html(options);
		});

		function getFloor(id) {
			var id = id ? id : 0;
			var result = null;
			$.ajax({
				url: "{BEESCRM::U('Common/getFloorList',array('account_id'=>$accountId))}",
				type: "post",
				dataType: 'json',
				async: false,
				data: {'id': id},
				success: function (data) {
					if (data.status == 'S') {
						result = data.data;
					}
				}
			});
			return result;
		}

		$('#floor_id').change(function () {
			var floor_id = $('#floor_id').val();
			var room = getRoom(floor_id);
			var options = [];
			var option = '<option value="0">请选择您所在的寝室</option>';
			options.push(option);
			if (room) {
				$.each(room, function (index, node) {
					var option = '<option value="' + node.room_id + '" data-name="' + node.room_name + '">' + node.room_name + '</option>';
					options.push(option);
				});
			}
			$("#room_id").html(options);
		});

		function getRoom(id) {
			var id = id ? id : 0;
			var result = null;
			$.ajax({
				url: "{BEESCRM::U('Common/getRoomList',array('account_id'=>$accountId))}",
				type: "post",
				dataType: 'json',
				async: false,
				data: {'id': id},
				success: function (data) {
					if (data.status == 'S') {
						result = data.data;
					}
				}
			});
			return result;
		}
	})
</script>
</body>
</html>